// @before-stub-for-debug-begin
#include <vector>
#include <string>
#include "commoncppproblem986.h"

using namespace std;
// @before-stub-for-debug-end

/*
 * @lc app=leetcode.cn id=986 lang=cpp
 *
 * [986] 区间列表的交集
 */

// @lc code=start
class Solution {
public:
    vector<vector<int>> intervalIntersection(vector<vector<int>>& firstList, vector<vector<int>>& secondList) {
        int i = 0, j = 0;
        vector<vector<int>> res;
        while (i < firstList.size() && j < secondList.size()) {
            int a1 = firstList[i][0], a2 = firstList[i][1], b1 = secondList[j][0], b2 = secondList[j][1];
            if (b2 >= a1 && a2 >= b1) {
                res.push_back({max(a1, b1), min(a2, b2)});
            }
            if (b2 < a2) {
                j++;
            } else {
                i++;
            }
        }
        return res;
    }
};
// @lc code=end

